<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增自然灾害题库')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-zrQuestionBank-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">题目：</label>
                    <div class="col-sm-8">
                        <input name="title" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">答案：</label>
                    <div class="col-sm-8">
                        <input name="answer" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">分类：</label>
                    <div class="col-sm-8">
                        <select id="disasterTypeId" name="disasterTypeId" class="form-control m-b">
                            <option th:each="dict : ${typeList}" th:text="${dict.typeName}" th:value="${dict.id}"></option>
                        </select>
                    </div>
                </div>
            </div>

            <h4 class="form-header h4">选项</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">

        $(function() {
            // 初始化数据, 可以由后台传过来
            var data = [
                {
                    id: "100",
                    selectStr: "A",
                    itemTitle: "",
                },
                {
                    id: "101",
                    selectStr: "B",
                    itemTitle: "",
                },
                {
                    id: "102",
                    selectStr: "C",
                    itemTitle: "",
                },
                {
                    id: "103",
                    selectStr: "D",
                    itemTitle: "",
                }
                ];
            var options = {
                data: data,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [
                    {
                        field: 'index',
                        align: 'center',
                        title: "序号",
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            var columnId = $.common.sprintf("<input type='hidden' name='list[%s].id' value='%s'>", index, row.id);
                            return columnIndex + $.table.serialNumber(index) + columnId;
                        }
                    },
                    {
                        field: 'selectStr',
                        align: 'center',
                        title: '选项',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control selectStr' type='text' name='list[%s].selectStr' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        field: 'itemTitle',
                        align: 'center',
                        title: '选项内容',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control itemTitle' type='text' name='list[%s].itemTitle' value='%s'>", index, value);
                            return html;
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                            return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                        }
                    }]
            };
            $.table.init(options);
        });
        var prefix = ctx + "ziran/zrQuestionBank"
        $("#form-zrQuestionBank-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-zrQuestionBank-add').serialize());
            }
        }
        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                selectStr: "",
                itemTitle: "",
            }
            sub.addRow(row);
        }
        $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
            $("input[name$='date']").datetimepicker({
                format: "yyyy-mm-dd",
                minView: "month",
                autoclose: true,
                pickerPosition:'top-right'
            });
        });
    </script>
</body>
</html>